﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblBookingDAO
    {
        private DBService _db = null;

        public TblBookingDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public bool Insert(TblBooking booking, out int IdOrder)
        {
            IdOrder = 0;
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblBooking_InsUp",
                   new string[] { "@OrderDate", "@CustomerIndex", "@Total", "@Status", "@SaleRep" },
                   new object[] { 
                                       Convert.ToDateTime(booking.OrderDate),
                                       Code.FixIntNull(booking.CustomerIndex),
                                        Convert.ToDecimal(booking.Total),
                                         Code.FixIntNull(booking.Status),
                                        booking.SaleRep }, CommandType.StoredProcedure);
                foreach (DataRow dr in dt.Rows)
                {
                    IdOrder = Convert.ToInt32(dr["IDOrder"].ToString());
                }
                IdOrder = IdOrder;
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public bool InsertBookingDetail(TblBookingDetail bookDetail)
        {
            bool flag = false;
            try
            {

                _db.ExeSP_DataTable("sp_TblBookingDetail_InsUp",
                  new string[] { "@IDOrder", "@ProductID", "@Quantity", "@UnitPrice", "@ExtendPrice", "@Discount" },
                  new object[] { bookDetail.IdOrder,  
                                    Code.FixIntNull(bookDetail.ProductId),
                                    Code.FixIntNull(bookDetail.Quantity),
                                    Convert.ToDecimal(bookDetail.UnitPrice),
                                    Convert.ToDecimal(bookDetail.ExtendPrice),
                                    Code.FixIntNull(bookDetail.Discount)},
                                    CommandType.StoredProcedure);
                flag = true;
           }
           catch (Exception ex)
           {
               throw ex;
              
           }
            return flag;
        }
      
         public DataTable GetBook_ByIDCustomer(int Customerindex)
         {
             DataTable dt = null;
             try
             {
                 dt = _db.ExeSP_DataTable("sp_GetOrder",
                     new string[] { "@CustomerIndex" },
                     new object[]{Customerindex}, CommandType.StoredProcedure);
             }
             catch (Exception ex)
             {
                 
                throw ex;
             }
             return dt;
         }

         public bool Insert(TblBooking booking, List<TblBookingDetail> lstDetail, out int BookID)
         {
             BookID = 0;

             bool flag = false;
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("sp_TblBooking_InsUp",
                    new string[] { "@OrderDate", "@CustomerIndex", "@Total", "@Status", "@SaleRep" },
                    new object[] { 
                                       Convert.ToDateTime(booking.OrderDate),
                                       Code.FixIntNull(booking.CustomerIndex),
                                        Convert.ToDecimal(booking.Total),
                                         Code.FixIntNull(booking.Status),
                                         booking.SaleRep
                                          
                 
                    }, CommandType.StoredProcedure);

                 foreach (TblBookingDetail bookingDetail in lstDetail)
                 {
                     foreach (DataRow dr in dt.Rows)
                     {
                         bookingDetail.ToatalAmount = bookingDetail.Quantity * bookingDetail.UnitPrice;

                         _db.ExeSP_DataTable("sp_TblBookingDetail_InsUp",
                           new string[] { "@IDOrder", "@ProductID", "@Quantity", "@UnitPrice", "@ExtendPrice", "@Discount", "@TotalAmount" },
                           new object[] { dr["IDOrder"],    
                                    Code.FixIntNull(bookingDetail.ProductId).ToString(),
                                 
                                    Code.FixIntNull(bookingDetail.Quantity).ToString(),
                                    Convert.ToDecimal(bookingDetail.UnitPrice), 
                                    Convert.ToDecimal(bookingDetail.ExtendPrice),
                                    Code.FixIntNull(bookingDetail.Discount).ToString(),
                                    Convert.ToDecimal(bookingDetail.ToatalAmount)
                                  
                          },
                                          CommandType.StoredProcedure);

                         BookID = Convert.ToInt32(dr["IDOrder"].ToString());

                     }
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return flag;
         }
    }
}
